import $ from './library/jquery.js';
import '../js/library/jquery.lazyload.js';
import cookie from './library/cookie.js';
// import $.extend from './library/jquery.lazyload.js';
$(function() {

    $('#ban-top>ul>li').not('.no').append('<span></span>')
    $('#ban-top>ul>li').not('.no').each(function(i, val) {
        let wid = $(this).width();
        wid = wid * 0.5 - 11.5 + 'px';
        $(this).children('span').css("left", wid);
    });

    // 1/6
    setInterval(function() {
        if (($('.slide-act>.swiper-slide-active').index() - 1) == 6) { $('.mmid span>strong').html(1); } else {
            $('.mmid span>strong').html(($('.slide-act>.swiper-slide-active').index()));

        }
    }, 300);
    $('.swip2>.swiper-button-next').on('click', function() {

        if (($('.slide-act>.swiper-slide-active').index() - 1) == 6) { $('.mmid span>strong').html(1); } else {
            $('.mmid span>strong').html(($('.slide-act>.swiper-slide-active').index()));

        }

        // })
        // $('.swip2>.swiper-button-prev').on('click', function() {
        //     $('.mmid span>strong').html($('.swiper-pagination-bullet-active').index() + 1);
        //tabindex="0"
    })
    $('.page2').on('click', 'span', function() {
        if (($('.slide-act>.swiper-slide-active').index() - 1) == 6) { $('.mmid span>strong').html(1); } else {
            $('.mmid span>strong').html(($('.slide-act>.swiper-slide-active').index()));

        }
    });
    //公告
    $('.gz>a').hover(function() {
        $(this).children().children().css('display', 'block');
        $(this).siblings().children().children().css('display', 'none');
        let ind = $(this).index();
        $($('.gz1').children()[ind]).addClass('act');
        $($('.gz1').children()[ind]).siblings().removeClass('act');
    })

    //侧边栏导航

    let flag = true;

    $('.aside>ul>li').on('mouseover', function() {

        if (flag) {
            $(this).children('.lt-hide').fadeIn(300);
            $(this).css('transition', 'all 0.3s');
            flag = !flag;
        } else {
            $(this).children('.lt-hide').css('display', 'block');
            $(this).css('transition', 'none');
        }
    });
    $('.aside').mouseleave(function() {
        flag = !flag;
    });
    $('.aside>ul>li').mouseleave(function() {
        $('.lt-hide').css('display', 'none');
    });

    //右侧楼梯定位 级顶部搜索固定定位
    $('.stik').css('right', (($('header').innerWidth()) - 1150) * 0.3);
    $('.stik>ul>li').on('click', function() {
        let elm = $(`#${$(this).children('a').attr('title')}`);
        let top = elm.offset().top - 48;

        $('html').animate({
            scrollTop: top
        }, 600);
    });
    $(window).on('scroll', function() {
        let top = $(document).scrollTop(),
            _index = 0;
        if (top < 670) {
            $('.stik>ul>li').eq(_index).addClass('active-r');
            $('.stik>ul>li').eq(_index).siblings().removeClass('active-r');
        } else if (top >= 670 && top < 1400) {
            _index = 1;
            $('.stik>ul>li').eq(_index).addClass('active-r');
            $('.stik>ul>li').eq(_index).siblings().removeClass('active-r');
        } else if (top >= 1400 && top < 2555) {
            _index = 2;
            $('.stik>ul>li').eq(_index).addClass('active-r');
            $('.stik>ul>li').eq(_index).siblings().removeClass('active-r');
        } else {
            _index = 3;
            $('.stik>ul>li').eq(_index).addClass('active-r');
            $('.stik>ul>li').eq(_index).siblings().removeClass('active-r');
        }
        if (top >= 456) {
            $('.stik').css('position', 'fixed')
            $('.stik').css('top', 54);
        } else {
            $('.stik').css('position', 'absolute')
            $('.stik').css('top', 510);
        }
        if (top >= 800) {
            $('.to-top').css('display', 'block');
        } else {
            $('.to-top').css('display', 'none');
        }
        if (top >= 196) {
            $('.st').addClass('sefix');
            $('.red-pack').css('visibility', 'hidden');
        } else {
            $('.st').removeClass('sefix');
            $('.red-pack').css('visibility', 'visible');
        }
    })



    //搜索选项切换
    $('#act>span').on('click', function() {
        if ($(this).hasClass('mid-mid')) {
            $('#sear-bt').css('visibility', 'hidden');
        } else {
            $('#sear-bt').css('visibility', 'visible');
        }
        $(this).addClass('act');
        $(this).siblings().removeClass('act');
    });


    //红包
    $('.red-pack>button').on('click', function() {
        $('.red-pack').css('display', 'none');
    });


    // ajax
    $.ajax({
        type: "get",
        url: "../../interface/getData.php",

        dataType: "json",
        success: function(res) {
            let temp = '';

            for (let i = 1; i < 4; i++) {
                res.forEach((elm, i) => {
                    let picture = JSON.parse(elm.picture);
                    temp += `<div class="guess pull-left">
                                <a href="./goods.html?id=${elm.id}" class="blimg">
                                    <img class="lazy" data-original="${picture[0].src}" alt="">
                                    <span></span>
                                </a>

                            <h4>${elm.title}</h4>
                            <p><span>￥</span>${elm.pr2}</p>
                            <a href="#">
                                <p><span class="glyphicon glyphicon-heart"></span>找相似</p>
                                <p>发现更多相似的宝贝 ></p>
                            </a>
                        </div>`;
                });
            }



            $('.like-list').html(temp);
            $('img.lazy').lazyload({
                placeholder: "../img/lazy.png",
                effect: "fadeIn"
            });

        }
    });
    //登录左上角

    if (cookie.get('username')) {
        $('.logtp').html(cookie.get('username'));
        $('.logtp').attr('href', 'javascript:;');
        $('.logtp').css('color', '#6c6c6c!important');
        $('.free').css('display', 'none');
        $('.bgr-top>span>i').html(cookie.get('username'));
        $('.log').css('display', 'none');
        $('.loged').css('display', 'block');
        $('.exit').css('display', 'block');
    } else {

        $('.logtp').html('亲，请登录 ');
        $('.logtp').attr('href', './login.html');
        $('.logtp').css('color', '#f22e00');
        $('.free').css('display', 'inline');
        $('.bgr-top>span>i').html('你好');
        $('.log').css('display', 'block');
        $('.loged').css('display', 'none');
        $('.exit').css('display', 'none');
    }
    //退出登录
    $('.exit').on('click', function() {
        let username = cookie.get('username'),
            car = JSON.stringify(cookie.get('shop'));
        $.ajax({
            type: "get",
            url: "../../interface/exit.php?username=" + username + "&car=" + car + "",

            success: function(res) {
                cookie.remove('username');
                cookie.remove('shop');
                cookie.remove('isLogined');
                location.href = './login.html';

            }
        });
    })

    //购物车数量
    if (cookie.get('shop') != 'null' && cookie.get('shop')) { $('.carnum').html(JSON.parse(cookie.get('shop')).length); }



})